$(function () {
    obj.init();
});

var obj = new function () {
    'use strict';
    return {
        dataGrid: null,
        dom: {
            list: null,
        },
        init: function () {
            this.dataGrid = new xhDataGrid($('#List'), 'Read/getArticleList');
            this.dataGrid.parseToolbar().init().activeBtn($('#bt_list_del, #bt_list_publish'), $('#bt_list_del_comment'));
            xhTool.bindDocKeyDown($('#bt_list_search'));
            this.form = new xhForm($('.xh-form'));
            this.form.parse().bindRequired($('#bt_form_add,#bt_form_edit'));
            this.dom.list = $('#List');
            this.listener();
        },
        listener: function () {
            var $this = this;
            //有修改权限，双击打开表单
            $this.dataGrid.onDblClickRow = function (rowIndex, rowData) {
                // if(rowData.releasestatus == '已发布'){
                //    window.top.xhTop.showMessage({
                //        title: '已发布文章不可修改！',
                //        type: 'error'
                //    });
                //    return false;
                // }
                window.top.xhTop.createNewTab('编辑文章', baseUrl + 'Read/articleAdd?id=' + rowData.id, true);
            };
            xhTool.bindDocKeyDown(
                $('#bt_list_search').click(function (){
                    $this.dataGrid.reload({
                        channelId: xhExtra.getValue($('#List_Search_Channel')),
                        articleTitle: xhExtra.getValue($('#List_Search_Article')),
                        startDate: xhExtra.getValue($('#list_start')),
                        endDate: xhExtra.getValue($('#list_end')),
                    })
                })
            );
            // 发布
            $('#bt_list_publish').click(function () {
                var ids = $this.dom.list.getDataGridSelectedFields('id');
                $this.funObj().changeStatus(ids, 'release', '是否确认发布？');
            });

            // 删除文章
            $('#bt_list_del').click(function () {
                var ids = $this.dom.list.getDataGridSelectedFields('id');
                $this.funObj().changeStatus(ids, 'del', '确定删除该文章吗？');
            });

            // 删除文章所有评论
            $('#bt_list_del_comment').click(function () {
                var id = $this.dom.list.getDataGridSelectedField('id');
                $this.funObj().changeStatus(id, 'delComment', '确认删除该文章所有评论吗？');
            });
        },
        funObj: function () {
            var $this = this;
            return {
                // 发布，删除和删除所有评论
                changeStatus: function (id, status, msg) {
                    var url = '';
                    var params = {};
                    if (id === '') {
                        window.top.xhTop.showError('未选中记录!');
                        return false;
                    }
                    switch (status) {
                        case 'del':
                            url = 'Read/delArticleData';
                            params = {ids: id};
                            break;
                        case 'delComment':
                            url = 'Read/delArticleComment';
                            params = {id: id, all: true};
                            break;
                        case 'release':
                            url = 'Read/publishArticle';
                            params = {ids: id};
                            break;
                    }
                    xhExtra.askChangeStatus(url, params, $this.dataGrid, msg);
                }
            }
        }
    }
};

// /**
//  * Created by CK on 2017/3/1.
//  */
// var list;
// $(document).ready(function () {
//         list = $('#List');
//     var type = xhTool.getQuery('type');
//     var btSearchDom = $('#bt_list_search');
//     $('.xh-datagrid-toolbar').parseToolbar();
//     $('.xh-form').parseForm($('#bt_form_add, #bt_form_edit'));
//     list.initDataGrid(baseUrl + 'Read/getArticleList').setSelectThenEnable(['bt_list_del', 'bt_list_publish'], ['bt_list_del_comment']);
//     DataGrid.onLoadSuccess = function(data){
//         list.dataGridClear();
//     };
//     /** 列表 **/
//     $('#bt_list_del').click(function () {
//         var ids = list.getDataGridSelectedFields('id');
//         $.overtFun.clickDelList(ids, baseUrl + 'Read/delArticleData');
//     });
//     $('#bt_list_publish').click(function () {
//         var ids = list.getDataGridSelectedFields('id');
//         $.overtFun.clickDelList(ids, baseUrl + 'Read/publishArticle', null, '是否确认发布？');
//     });
//     DataGrid.onDblClickRow = function(rowIndex, rowData){
//         //if(rowData.releasestatus == '已发布'){
//         //    window.top.xhTop.showMessage({
//         //        title: '已发布文章不可修改！',
//         //        type: 'error'
//         //    });
//         //    return false;
//         //}
//         window.top.xhTop.createNewTab('编辑文章', baseUrl + 'Read/articleAdd?id=' + rowData.id, true);
//     };
//     btSearchDom.click(function(){
//         list.addDataGridParams({
//             channelId : $('#List_Search_Channel').getElementValue(),
//             articleTitle: $('#List_Search_Article').getElementValue(),
//             startDate: xhExtra.getValue($('#list_start')),
//             endDate: xhExtra.getValue($('#list_end')),
//         });
//     });
//     xhTool.bindDocKeyDown(btSearchDom);
//     $('#bt_list_del_comment').click(function () {
//         var id = list.getDataGridSelectedField('id');
//         xhExtra.askChangeStatus('Read/delArticleComment', {id: id, all: true}, null, '确认删除该文章所有评论？');
//     });
// });
